package com.wk.tif;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class TifAnalyze {
	private int ncols ;//列
	private int nrows;//行
	private double xllcorner;
	private double yllcorner;
	private int cellsize;
	private int NODATA_VALUE;

	private int line=0;
	private double[][] datas ;
	private String PROJCS;

	public TifAnalyze(File tifFile){
		FileReader fileReader;
		try {
			fileReader = new FileReader(tifFile);
			BufferedReader bufferFileR = new BufferedReader(fileReader);
			initTifHead(bufferFileR,1);
			
			initTifDatas(bufferFileR,7);
			initPROJCS(bufferFileR, 7+this.nrows);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	
	@Override
	public String toString() {
		return "TifAnalyze [ncols=" + ncols + ", nrows=" + nrows + ", xllcorner=" + xllcorner + ", yllcorner="
				+ yllcorner + ", cellsize=" + cellsize + ", NODATA_VALUE=" + NODATA_VALUE + ", PROJCS=" + PROJCS + "]";
	}


	private void  initTifHead(BufferedReader bufferFileR,int beginLine){
		try {
			int lineNumber =0;
			String tmpStr=null;
			while((tmpStr = bufferFileR.readLine())!=null){
				lineNumber++;
				String tmpArr[] = tmpStr.trim().split("\t");
				switch(lineNumber ){
				case 1 :
					this.ncols = Integer.valueOf(tmpArr[tmpArr.length-1]);
					break;
				case 2:
					this.nrows = Integer.valueOf(tmpArr[tmpArr.length-1]);
					break;
				case 3:
					this.xllcorner = Double.valueOf(tmpArr[tmpArr.length-1]);
					break;
				case 4:
					this.yllcorner = Double.valueOf(tmpArr[tmpArr.length-1]);
					break;
				case 5:
					this.cellsize = Integer.valueOf(tmpArr[tmpArr.length-1]);
					break;
				case 6:
					this.NODATA_VALUE = Integer.valueOf(tmpArr[tmpArr.length-1]);
					break;
				default:
					break;
				}
				if(lineNumber>=6){
					break ;//只读取前六行
				}
			}
		} catch (Exception e) {
			System.out.println("初始化initTifHead失败！");
			e.printStackTrace();
		}
	}
	private void initTifDatas(BufferedReader bufferFileR,int beginLine){
		this.datas = new double[this.nrows][this.ncols];
		int lineNumber =0;
		try {
			String tmpStr=null;
			while((tmpStr = bufferFileR.readLine())!=null){
				
				String tmpArr[] = tmpStr.trim().split(" ");
				for(int i=0;i<tmpArr.length;i++){
					double tmp = Double.valueOf(tmpArr[i]);
					this.datas[lineNumber][i] = tmp;
				}
				lineNumber++;
				if(lineNumber>=this.nrows) {
					break ;//只读取开始行和数据行之间的数
				}
			}
						
		} catch (Exception e) {
			System.out.println("初始化initTifDatas失败！");
			System.out.println(lineNumber);
			e.printStackTrace();
		}
		
	}
	private void initPROJCS(BufferedReader bufferFileR,int beginLine){
		try {
			String tmpStr=null;
			while((tmpStr = bufferFileR.readLine())!=null){
				PROJCS = tmpStr;
			}
						
		} catch (Exception e) {
			System.out.println("初始化initPROJCS失败！");
			e.printStackTrace();
		}
	}
	
	
	public int getNcols() {
		return ncols;
	}


	public int getNrows() {
		return nrows;
	}


	public double getXllcorner() {
		return xllcorner;
	}


	public double getYllcorner() {
		return yllcorner;
	}


	public int getCellsize() {
		return cellsize;
	}


	public int getNODATA_VALUE() {
		return NODATA_VALUE;
	}

	public String getPROJCS() {
		return PROJCS;
	}
	
}
